package apy.codecase.business.view.activity;

import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import apy.codecase.R;
import apy.codecase.adapter.MainVpAdapter;
import apy.codecase.base.BaseActivity;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * 主界面
 */
public class MainActivity extends BaseActivity {

    @BindView(R.id.main_vp)
    ViewPager mainVp;
    @BindView(R.id.main_psw_root)
    TextView mainPswRootTv;
    @BindView(R.id.main_tally_root)
    TextView mainTallyRootTv;
    @BindView(R.id.main_diary_root)
    TextView mainDiaryRootTv;
    @BindView(R.id.main_me_root)
    TextView mainMeRootTv;
    //底部导航文字
    private int[] bottomText = {R.string.main_bottom_item1, R.string.main_bottom_item2, R.string.main_bottom_item3, R.string.main_bottom_item4};
    private List<TextView> mTvList = new ArrayList<>();

    @Override
    protected String title() {
        return null;
    }

    @Override
    protected boolean controlTitle() {
        return false;
    }

    @Override
    protected int getViewId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initData() {
        mainPswRootTv.setSelected(true);
        initVpAdapter();
        initTv();
    }

    private void initTv() {
        mTvList.add(mainPswRootTv);
        mTvList.add(mainTallyRootTv);
        mTvList.add(mainDiaryRootTv);
        mTvList.add(mainMeRootTv);
        mainPswRootTv.setText(bottomText[0]);
        mainTallyRootTv.setText(bottomText[1]);
        mainDiaryRootTv.setText(bottomText[2]);
        mainMeRootTv.setText(bottomText[3]);
    }

    @OnClick({R.id.main_psw_root, R.id.main_tally_root, R.id.main_diary_root, R.id.main_me_root})
    public void onViewClicked(View view) {
        for (int i = 0; i < mTvList.size(); i++) {
            mTvList.get(i).setSelected(false);
        }
        switch (view.getId()) {
            case R.id.main_psw_root:
                mainPswRootTv.setSelected(true);
                mainVp.setCurrentItem(0, false);
                break;
            case R.id.main_tally_root:
                mainTallyRootTv.setSelected(true);
                mainVp.setCurrentItem(1, false);
                break;
            case R.id.main_diary_root:
                mainDiaryRootTv.setSelected(true);
                mainVp.setCurrentItem(2, false);
                break;
            case R.id.main_me_root:
                mainMeRootTv.setSelected(true);
                mainVp.setCurrentItem(3, false);
                break;
        }
    }

    /**
     * 初始化ViewPager的Adapter
     * 和底部导航联动
     */
    private void initVpAdapter() {
        MainVpAdapter mainVpAdapter = new MainVpAdapter(getSupportFragmentManager(), bottomText);
        mainVp.setAdapter(mainVpAdapter);
        mainVp.setOffscreenPageLimit(bottomText.length);
        mainVp.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                for (int i = 0; i < mTvList.size(); i++) {
                    mTvList.get(i).setSelected(false);
                }
                mTvList.get(position).setSelected(true);
            }
        });
    }
}
